<template>
  <div id="app">
    <el-form :inline="true" :model="name" class="demo-form-inline">
      <el-form-item label="用户姓名">
        <el-input v-model="name" placeholder="请输入用户姓名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selectByName">查询</el-button>
        <el-button type="primary" @click="addUser">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="users"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="140">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="150">
      </el-table-column>
      <el-table-column
        prop="age"
        label="年龄"
        width="150">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="160">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" @click="bangding(scope.row.id)"></el-button>
          <el-button type="primary" icon="el-icon-delete" @click="del(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[2, 3, 4, 5]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" v-show="total!=0">
    </el-pagination>
    <el-dialog
      title="编辑用户信息"
      :visible.sync="dialogVisible"
      width="80%"
      :before-close="handleClose">
      <el-form :model="user" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户id">
          <el-input v-model="user.id" readonly></el-input>
        </el-form-item>
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="user.age"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">立即编辑</el-button>
          <el-button  @click="resetForm(user)">重置</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer"></span>
    </el-dialog>
  </div>
</template>


<script>
  export default {
    name: 'app',
    data: function () {
      return {
        //用户集合
        users: [],
        //当前页码
        current: 1,
        //每页显示条数
        pageSize: 2,
        //姓名模糊查询
        search: {
          name: null
        },
        //总记录数
        total: 100,
        //是否编辑
        dialogVisible: false,
        user: {
        },
        rules: {
          name: [
            {required: true, message: '请输入用户名称', trigger: 'blur'}
          ],
          age: [
            {required: true, message: '请输入用户年龄', trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入用户邮箱', trigger: 'blur'}
          ]
        }
      }
    },
    created: function () {
      var $this = this;
      $this.query($this.current, $this.pageSize);
    },
    methods: {
      query(current, pageSize, name) {
        var $this = this;
        $this.axios.get('http://localhost:8082/query',
          {
            params: {
              current: current,
              name: name,
              pageSize: pageSize
            }
          })
          .then(function (response) {
            $this.users = response.data.records;
            $this.total = response.data.total;
          })
      },
      //每页显示条数
      handleSizeChange(val){
        var $this = this;
        $this.pageSize = val;
        $this.query($this.current, $this.pageSize, $this.search.name);
      },
      handleCurrentChange(val) {
        var $this = this;
        $this.current = val;
        $this.query($this.current, $this.pageSize, $this.search.name);
      },
      bangding(id) {
        var $this = this;
        $this.dialogVisible = true;
        $this.axios.get('http://localhost:8082/selectOne',
          {
            params: {
              id: id
            }
          })
          .then(function (response) {
            $this.user = response.data;
          })
      },
      del(id) {
        var $this = this;
        $this.axios.delete('http://localhost:8082/delete',
          {
            params: {
              id: id
            }
          })
          .then(function (response) {
            if (response.data == true) {
              $this.query($this.current, $this.pageSize, $this.search.name)
            } else {
              alert('删除失败');
            }
          })
      },
      addUser() {
        this.dialogVisible = true;
        this.user={};
      },

      //提交编辑
      submitForm() {
        var $this = this;
        $this.axios.post('http://localhost:8082/edit', $this.user)
          .then(function (response) {
            $this.dialogVisible = false;
            $this.user = {};
            $this.query($this.current, $this.pageSize, $this.search.name);
          })
      },
      //重置
      resetForm(formName){
        if (this.$refs[formName]!=undefined) {
          this.$refs[formName].resetFields();
        }
      },
      //模糊查询
      selectByName() {
        var $this = this;
        $this.query(this.current, this.pageSize, this.search.name);
      },
      //关闭弹窗
      handleClose(done) {
        var $this = this;
        $this.dialogVisible = false;
      }
    }
  }
</script>
<style>
  #app {
    width: 800px;
    height: 500px;
    border: 1px solid #DCDFE6;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04)
  }
</style>
